//
//  MyViewController.swift
//  声网demo
//
//  Created by 秦江涛 on 2022/9/8.
//

import UIKit

class MyViewController: UIViewController {
    
    private var avatarBtn = UIButton()
    private var nameL = UILabel()
    private var titleArr = [String]()
    private var userInfoModel = QIUserModel()
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]
        getUserInfoData()
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.black]
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        title = "个人中心"

        addUI()
    }
    
    
    private func getUserInfoData() {
        QINetworkManager.requestJson(urlStr: "/User/getUserInfo", method: .get, parameters: [:]) { json in
            print(json)
            self.userInfoModel = json.dictionaryObject?.kj.model(QIUserModel.self) ?? QIUserModel()
            self.avatarBtn.kf.setImage(with: URL(string: "\(IMAGE_URL)\(self.userInfoModel.avatar)"), for: .normal)
            self.nameL.text = self.userInfoModel.nickname
            
            if let data = try? JSONEncoder().encode(json) {
                print("成功将JSON转换为Data")
                Defaults[\.userData] = data
            } else {
                print("无法将JSON转换为Data")
            }
        }
        
    }
    
    private func addUI() {
        view.backgroundColor = ColorEEEEEE
        titleArr = ["用户协议", "隐私协议", "联系客服", "意见反馈", "关于我们", "设置"]
        
        let headerImgView = UIImageView().then {
            $0.frame = CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 230)
            $0.backgroundColor = UIColor.gradientColor(from: MainColor, to: .white, withHeight: 230)
            $0.image = UIImage(named: "")
            $0.contentMode = .scaleAspectFill
            $0.clipsToBounds = true
        }
        view.addSubview(headerImgView)
        
        let headerView = UIView()
        headerView.frame = CGRect(x: 18, y: NAV_HEIGHT + 64, width: SCREEN_WIDTH - 36, height: 112)
        headerView.backgroundColor = .white
        headerView.addRoundedCorners(withRadius: 18)
        view.addSubview(headerView)
        
        avatarBtn = UIButton().then {
            $0.frame = CGRect(x: SCREEN_WIDTH / 2 - 36, y: NAV_HEIGHT + 30, width: 72, height: 72)
            $0.setImage(UIImage(named: "noo"), for: .normal)
            $0.imageView?.contentMode = .scaleAspectFill
            $0.addRoundedCorners(withRadius: 36)
            $0.addTarget(self, action: #selector(avatarBtnClick), for: .touchUpInside)
        }
        view.addSubview(avatarBtn)
        
        nameL = UILabel().then({
            $0.frame = CGRect(x: 30, y: avatarBtn.bottom + 14, width: SCREEN_WIDTH - 60, height: 25)
            $0.text = "风轻云淡"
            $0.textAlignment = .center
            $0.textColor = Color333333
            $0.font = SYSFONT(ofSize: 18)
        })
        view.addSubview(nameL)
        
        view.addSubview(tableView)
    }
    
    @objc private func avatarBtnClick() {
        let ctl = PersonalDataViewController()
        ctl.userModel = userInfoModel
        navigationController?.pushViewController(ctl, animated: true)
    }
    
    
    private lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.frame = CGRect(x: 17, y: NAV_HEIGHT + 190, width: SCREEN_WIDTH - 34, height: 300)
        tableView.backgroundColor = .white
        tableView.delegate = self
        tableView.dataSource = self
        tableView.addRoundedCorners(withRadius: 10)
        if #available(iOS 15.0, *) {
            tableView.sectionHeaderTopPadding = 0
        } else {}
        return tableView
    }()

}

extension MyViewController: UITableViewDataSource, UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return titleArr.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        50
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell()
        cell.selectionStyle = .none
        cell.accessoryType = .disclosureIndicator
        let titleStr = titleArr[indexPath.row]
        cell.imageView?.image = UIImage(named: titleStr)
        cell.textLabel?.text = titleStr
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let titleStr = titleArr[indexPath.row]
        if titleStr == "用户协议" {
            let ctl = ProtocolViewController()
            ctl.titleStr = titleStr
            navigationController?.pushViewController(ctl, animated: true)
        } else if titleStr == "隐私协议" {
            let ctl = ProtocolViewController()
            ctl.titleStr = titleStr
            navigationController?.pushViewController(ctl, animated: true)
        } else if titleStr == "联系客服" {
            ShowHUD(str: "功能开发中")
        } else if titleStr == "意见反馈" {
            let ctl = FeedbackViewController()
            navigationController?.pushViewController(ctl, animated: true)
        } else if titleStr == "关于我们" {
            let ctl = AboutViewController()
            navigationController?.pushViewController(ctl, animated: true)
        } else if titleStr == "设置" {
            let ctl = SetViewController()
            navigationController?.pushViewController(ctl, animated: true)
        } else if titleStr == "" {
            
        }
    }
    
}
